@extends('admin.layouts.app')
@section('css')
 @parent
    <link href="{{asset('css/sweetalert.css')}}" rel="stylesheet">
@endsection
@section('content')
<div class="content-wrapper">
  <!-- Content Header (Page header) -->
  <section class="content-header">
        <span>权限创建</span>
      </section>
  
      <!-- Main content -->
      <section class="content">
        <div class="row">
          <div class="col-md-12">
            <!-- AREA CHART -->
            <div class="box box-primary" style="border-top-color:transparent;">
              <div class="box-body">
                <div class="chart ss-main">
                      <form class="form-horizontal">
                       <input type ="hidden" name="csrf-token" value ="{{ csrf_token() }}">
                      <div class="form-group">
                          <label for="inputPassword3" class="col-sm-1 control-label">备注名</label>
                          <div class="col-sm-11">
                          <input type="text" name="name" class="form-control" placeholder="备注名">
                          </div>
                      </div>
                      <div class="form-group">
                          <label for="inputPassword3" class="col-sm-1 control-label">节点</label>
                          <div class="col-sm-11">
                          <input type="text" name="code"  class="form-control" placeholder="节点">
                          </div>
                      </div>
                     
                      <div class="form-group">
                          <div class="col-sm-offset-1 col-sm-11">
                              <a href="{{admin_base_path('permissions')}}">
                              <button type="button" class="btn btn-primary">返回</button>
                              </a>
                              <button type="button" id="btn-create" class="btn btn-primary">提交</button>
                          </div>
                      </div>
                      </form>
  
                </div>
              </div>
              <!-- /.box-body -->
            </div>
            
  
          </div>
        </div>
        <!-- /.row -->
  
      </section>
</div>
@endsection
@section('js')
 @parent
<script src="{{asset('js/sweetalert.min.js')}}"></script>
 
<script>
    (function() {
        var $navli = $("#jc_nav li"),
            $showbox = $(".mark_show");
        $navli.on("click",function() {
            $(this).addClass("active").siblings().removeClass("active");
            $showbox.hide();
            $showbox.eq( $(this).index() ).show();
        });
    })();

    (function() {
        var $navli = $("#jc_nav li"),
            $showbox = $(".mark_show");
        $navli.on("click",function() {
            $(this).addClass("active").siblings().removeClass("active");
            $showbox.hide();
            $showbox.eq( $(this).index() ).show();
        });
    })();
    //ajax请求
    function getData(obj) {
        var data = {},
            $tmp = null;
        for (var attr in obj) {
            var list = obj[attr];
            if (attr=='radio') {
                list.forEach(function(v,i) {
                    var val = $('input[name="'+ v +'"]:checked').val();
                    data[v] = val==void 0 ? "" : val;
                });
            } else if (attr=='checkbox') {
                list.forEach(function(v,i) {
                    var arr = [];
                    $tmp = $('input[name="'+ v +'"]');
                    $tmp.each(function(i,v) {
                        if ( $(this)[0].checked ) {
                            arr.push( $(this).val() )
                        }
                    });
                    data[v] = arr;
                });
            } else {
                list.forEach(function(v,i) {
                    var val = $('input[name="'+ v +'"]').val();
                    data[v] = val;
                });
            }
        }
        return data;
    }
    var obj = {
        'text': ['code','name'],
        // 'radio': ['group'],
        // 'checkbox': ['test3'],
    }
    
    // 获取数据
    $("#btn-create").on("click", function() {
        var data = getData(obj);
        console.log(data);
        $.ajax({
            url: "{{admin_base_path('permissions')}}",
            type: 'POST',
            headers: {
            'X-CSRF-TOKEN': $('input[name="csrf-token"]').attr('value')
             },  
            data: data,
            success: function(data) {

                if(data.code=='0')
                {
                  swal({ 
                        title: "干得漂亮！", 
                        type: "success",
                      },
                      function(obj){
                        if(obj)
                        {
                          location.href="{{admin_base_path('permissions')}}"
                        }
                      });
                }
                else
                {
                  sweetAlert("哎呦……", "出错了！","error");
                }
            },
        })
    });

</script>
@endsection